package com.qining618.cms.ucenter.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qining618.cms.ucenter.controller.admin.request.AdminRoleEditRequest;
import com.qining618.cms.ucenter.controller.admin.request.AdminRoleSetMenuRequest;
import com.qining618.cms.ucenter.domain.Role;
import com.qining618.cms.ucenter.service.RoleService;
import com.qining618.common.annotation.Access;
import com.qining618.common.result.ApiResult;
import com.qining618.common.result.TableData;
import com.qining618.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author qining
 * @since 2019-09-16
 */
@RestController
@RequestMapping("/admin/api/ucenter/role")
public class AdminRoleController {

    @Autowired
    private RoleService roleService;

    @Access(needAdmin = true)
    @GetMapping(value = "page")
    @Description("分页")
    public ApiResult page(@RequestParam Map<String, Object> params) {
        Query<Role> query = new Query<>(params);

        IPage<Role> pageInfo = roleService.page(query.getPage(), query.getQueryWrapper());

        return ApiResult.ok(TableData.of(pageInfo));
    }

    @Access(needAdmin = true)
    @GetMapping(value = "list")
    @Description("列表")
    public ApiResult list(@RequestParam Map<String, Object> params) {
        Query<Role> query = new Query<>(params);

        List<Role> roleList = roleService.list(query.getQueryWrapper());

        return ApiResult.ok(roleList);
    }

    @Access(needAdmin = true)
    @PostMapping
    @Description("添加")
    public ApiResult add(@RequestBody @Validated AdminRoleEditRequest roleEditRequest) {
        Role role = roleService.add(roleEditRequest.getName());
        return ApiResult.ok(role);
    }

    @Access(needAdmin = true)
    @PutMapping(value = "{id}")
    @Description("编辑")
    public ApiResult edit(
            @PathVariable(value = "id") Long id,
            @RequestBody @Validated AdminRoleEditRequest roleEditRequest) {
        Role role = roleService.edit(id, roleEditRequest.getName());
        return ApiResult.ok(role);
    }

    @Access(needAdmin = true)
    @DeleteMapping(value = "{id}")
    @Description("删除")
    public ApiResult delete(@PathVariable(value = "id") Long id) {
        boolean result = roleService.removeById(id);
        if (!result) {
            return ApiResult.failed();
        }
        return ApiResult.ok();
    }

    @Access(needAdmin = true)
    @PostMapping(value = "{id}/menu")
    @Description("设置权限菜单")
    public ApiResult setMenu(
            @PathVariable(value = "id") Long id,
            @RequestBody @Validated AdminRoleSetMenuRequest roleSetMenuRequest) {
        roleService.setMenuIds(id, roleSetMenuRequest.getMenuIds());
        return ApiResult.ok();
    }
}

